package com.monitor.controller;

import com.monitor.commons.SkList;
import com.monitor.commons.StringUtil;
import com.monitor.commons.redis.RedisUtil;
import com.monitor.pubInter.*;
import com.monitor.pubInter.baseVO.AbstractDataVO;
import com.monitor.pubInter.baseVO.AbstractPageVO;
import com.monitor.pubInter.baseVO.DecorateModel;
import com.monitor.pubInter.baseVO.DecoratePageList;
import com.monitor.web.common.token.JwtsUtil;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunke
 * @Date 2020-12-14 19:59:07
 * @description
 */

public abstract class ApiManageController<DTO extends AbstractDTO
        , PageDTO extends AbstractPageDTO
        , T extends BaseIdDoMain<KeyType>
        , TS extends BaseSearch
        , DomainVO extends AbstractDomainVO<KeyType>
        , DetailDomainVO extends DomainVO
        , DetailVO extends AbstractDataVO<DetailDomainVO>
        , ListVO extends AbstractPageVO<DetailDomainVO>
        , KeyType
        > extends RestfulController<DTO, PageDTO, T, TS, DomainVO, DetailDomainVO, DetailVO, ListVO, KeyType> {


    private static boolean debug;
    private static boolean imgCodeVerification;
    private static String fileBaseUrl;
    private static JwtsUtil jwtsUtil;
    private static RedisUtil redisUtil;


    public static boolean isDebug() {
        return debug;
    }

    @Value("${debug}")
    public void setDebug(boolean debug) {
        ApiManageController.debug = debug;
    }

    public static boolean isImgCodeVerification() {
        return imgCodeVerification;
    }

    @Value("${imgCodeVerification}")
    public void setImgCodeVerification(boolean imgCodeVerification) {
        ApiManageController.imgCodeVerification = imgCodeVerification;
    }

    public static String getFileBaseUrl() {
        return fileBaseUrl;
    }

    @Value("${file.upload.website}")
    public void setFileBaseUrl(String fileBaseUrl) {
        ApiManageController.fileBaseUrl = fileBaseUrl;
    }

    /**
     * 得到完整的图片列表
     *
     * @param img
     * @return
     */
    public static String getIntactImg(String img) {

        if (StringUtil.isEmpty(img)) {
            return "";
        }
        if (img.toLowerCase().startsWith("http")) {
            return img;
        }

        return ApiManageController.getFileBaseUrl() + "/" + img;

    }

    /**
     * 得到完整的图片列表
     *
     * @param imgs
     * @return
     */
    public static List<String> getIntactImg(List<String> imgs) {

        if (StringUtil.isNullOrEmpty(imgs)) {
            return null;
        }

        return imgs.stream().map(
                item -> {

                    return ApiManageController.getIntactImg(item);

                }).collect(Collectors.toList());


    }

    /**
     * 得到完整的图片列表
     *
     * @param img
     * @return
     */
    public static List<String> getImgListByImgStr(String img) {

        List<String> imgs = null;
        if (StringUtil.isNotEmpty(img)) {

            String[] imgList = img.split(",");

            imgs = new ArrayList<>();
            for (String item : imgList) {
                imgs.add(ApiManageController.getFileBaseUrl() + "/" + item);
            }

        }

        return imgs;

    }

    public static RedisUtil getRedisUtil() {
        return ApiManageController.redisUtil;
    }

    @Resource(name = "redisUtil")
    public void setRedisUtil(RedisUtil redisUtil) {
        ApiManageController.redisUtil = redisUtil;
    }

    public static JwtsUtil getJwtsUtil() {
        return ApiManageController.jwtsUtil;
    }

    @Resource(name = "jwtsUtil")
    public void setJwtsUtil(JwtsUtil jwtsUtil) {
        ApiManageController.jwtsUtil = jwtsUtil;
    }





    public Integer getCount(String redisKey, List<String> rightsList) {

        if (StringUtil.isNullOrEmpty(rightsList)) {
            return 0;
        }

        Map<String, Integer> dataMap = getRedisUtil().hmget(redisKey);
        if (StringUtil.isNullOrEmpty(dataMap)) {
            return 0;
        }


        String key = null;
        int count = 0;

        for (Map.Entry<String, Integer> entry : dataMap.entrySet()) {
            key = entry.getKey();
            String finalKey = key;
            if (rightsList.stream().anyMatch(frontValue -> finalKey.equalsIgnoreCase(frontValue))) {
                count = count + entry.getValue();
            }
        }

        return count;

    }


    public <T, V> V mapDetail(DecorateModel<T> decorateModel, Class<V> detailVOClass) {
        return map(decorateModel, detailVOClass);
    }

    public <T, V> V mapDetail(T model, Class<V> detailVOClass) {
        return map(new DecorateModel<T>(model), detailVOClass);
    }

    public <T, V> List<V> mapListDetail(List<T> modelList, Class<V> detailVOClass) {
        if (StringUtil.isObjEmpty(modelList)) {
            return null;
        }
        List<V> returnList = SkList.getInstance();
        modelList.stream().forEach(t -> {
            returnList.add(map(new DecorateModel<T>(t), detailVOClass));
        });

        return returnList;

    }

    public <T, D, V> V mapDetail(T data, Class<D> domainVOClass, Class<V> detailVOClass) {

        return map(new DecorateModel(domainVOClass), detailVOClass);

    }

    public <T, V> V mapList(DecoratePageList<T> decoratePageList, Class<V> listVOClass) {

        return map(decoratePageList, listVOClass);

    }


}
